/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package upgmadendro.utils;

import upgmadendro.graph.*;
import java.util.ArrayList;
import upgmadendro.population.Population;
import upgmadendro.utils.DistanceCalc;

/**
 *
 * @author Shadev
 */
public class DistanceGraphGenerator {
    DistanceCalc distanceCalculator = null;
    
    public DistanceGraphGenerator(DistanceCalc d){
        this.distanceCalculator=d;
    }

    public DistanceGraph generateGraph(ArrayList<Population> pop){
        DistanceGraph res = new DistanceGraph();
        this.addVertice(pop, res);
        int i=0;
        double dist=0;
        Population currentPop = null;
        while(i<pop.size()){
            currentPop=pop.get(i);
            for(int j=i+1;j<pop.size();j++){
                dist = this.distanceCalculator.getDistance(currentPop, pop.get(j));
                this.addEdge(res.getVertice().get(i), res.getVertice().get(j), dist, res);
            }
            i++;
        }
        return res;
    }

    private void addVertice(ArrayList<Population> pop,DistanceGraph g){
        for(int i=0;i<pop.size();i++){
            g.getVertice().add(new Vertex(pop.get(i).getName()));
        }
    }

    private void addEdge(Vertex a,Vertex b,double distance,DistanceGraph g){
        Edge e = new Edge();
        e.setValue(distance);
        e.getVertice()[0]=a;
        e.getVertice()[1]=b;
        a.getEdges().add(e);
        b.getEdges().add(e);
        g.getEdges().add(e);
    }
}
